﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TickCaptures.cs" company="">
//   
// </copyright>
// <summary>
//   The tick captures.
// </summary>
// --------------------------------------------------------------------------------------------------------------------


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;

namespace Systemathics.FrameWork
{
    [Serializable]
    public sealed class TickCaptures : IDictionary<DateTime, TickCapture>, ISerializable, IDeserializationCallback
    {
        private readonly Dictionary<DateTime, TickCapture> series;

        public TickCaptures()
        {
            series = new Dictionary<DateTime, TickCapture>();
        }
        public TickCaptures(Dictionary<DateTime, TickCapture> t)
        {
            series = t;
        }
        public TickCaptures(SerializationInfo info, StreamingContext context)
        {
            SerializationReader sr = SerializationReader.GetReader(info);
            series = sr.ReadDictionary();
        }

        #region IDeserializationCallback Members

        public void OnDeserialization(object sender)
        {
        }

        #endregion

        #region IDictionary<DateTime,TickCapture> Members

        public void Add(DateTime key, TickCapture value)
        {
            series.Add(key, value);
        }
        public bool ContainsKey(DateTime key)
        {
            return series.ContainsKey(key);
        }
        public ICollection<DateTime> Keys
        {
            get { return series.Keys; }
        }
        public bool Remove(DateTime key)
        {
            return series.Remove(key);
        }
        public bool TryGetValue(DateTime key, out TickCapture value)
        {
            return series.TryGetValue(key, out value);
        }
        public ICollection<TickCapture> Values
        {
            get { return series.Values; }
        }
        public TickCapture this[DateTime key]
        {
            get { return series[key]; }
            set { series[key] = value; }
        }
        public void Add(KeyValuePair<DateTime, TickCapture> item)
        {
            ((ICollection<KeyValuePair<DateTime, TickCapture>>) series).Add(item);
        }
        public void Clear()
        {
            ((ICollection<KeyValuePair<DateTime, TickCapture>>) series).Clear();
        }
        public bool Contains(KeyValuePair<DateTime, TickCapture> item)
        {
            return ((ICollection<KeyValuePair<DateTime, TickCapture>>) series).Contains(item);
        }
        public void CopyTo(KeyValuePair<DateTime, TickCapture>[] array, int arrayIndex)
        {
            ((ICollection<KeyValuePair<DateTime, TickCapture>>) series).CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return series.Count(); }
        }
        public bool IsReadOnly
        {
            get { return ((ICollection<KeyValuePair<DateTime, TickCapture>>) series).IsReadOnly; }
        }
        public bool Remove(KeyValuePair<DateTime, TickCapture> item)
        {
            return ((ICollection<KeyValuePair<DateTime, TickCapture>>) series).Remove(item);
        }
        public IEnumerator<KeyValuePair<DateTime, TickCapture>> GetEnumerator()
        {
            return series.GetEnumerator();
        }
        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable) series).GetEnumerator();
        }

        #endregion
         
        #region ISerializable Members

        /// <summary>
        /// The get object data.
        /// </summary>
        /// <param name="info">
        /// The info.
        /// </param>
        /// <param name="context">
        /// The context.
        /// </param>
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            SerializationWriter sw = SerializationWriter.GetWriter();
            sw.Write(series);
            sw.AddToInfo(info);
        }

        #endregion

        public void AddRange(TickCaptures tc)
        {
            foreach (var i in tc)
                series.Add(i.Key, i.Value);
        }
        public TickCaptures DeepCopy()
        {
            var clone = new Dictionary<DateTime, TickCapture>();
            foreach (KeyValuePair<DateTime, TickCapture> kvp in series)
                clone[kvp.Key] = kvp.Value;

            return new TickCaptures(clone);
        }
    }
}